package com.mobile.liuyi_s.orderlunch.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.mobile.liuyi_s.orderlunch.R;
import com.mobile.liuyi_s.orderlunch.entity.Order;

import java.util.Collection;
import java.util.List;

/**
 * Created by liuyi_s on 2017/8/16 15:52.
 * Email：liuyi_s@foxmail.com
 */

public class OrderListAdapter extends RecyclerView.Adapter<OrderListAdapter.MyViewHolder> implements View.OnClickListener {

    private List<Order> mOrders;
    private Context mContext;
    private onChildClickListener mClickListener;
    private RecyclerView mRecyclerView;
    private static final String TAG = "OrderListAdapter";

    public OrderListAdapter(Context context, List<Order> orders ) {
        mOrders = orders;
        mContext = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext)
                .inflate(R.layout.item_view_recycler_order_list, parent, false);
        view.setOnClickListener(this);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.mTextViewUsername.setText("姓名："+mOrders.get(position).getOrderUser());
        holder.mTextViewOrderNum.setText("份数："+String.valueOf(mOrders.get(position).getOrderNum()));
    }

    @Override
    public int getItemCount() {
        return mOrders.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        private ImageView mImageViewCover;
        private TextView mTextViewUsername;
        private TextView mTextViewOrderNum;

        public MyViewHolder(View itemView) {
            super(itemView);
            mImageViewCover = itemView.findViewById(R.id.img_item_view_recycler);
            mTextViewUsername = itemView.findViewById(R.id.txt_username_item_view);
            mTextViewOrderNum = itemView.findViewById(R.id.txt_ordernum_item_view);
        }
    }

    public void addAll(Collection<? extends Order> collection) {
        mOrders.addAll(collection);
        notifyDataSetChanged();
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        this.mRecyclerView = recyclerView;
    }

    @Override
    public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
        this.mRecyclerView = null;
    }

    @Override
    public void onClick(View view) {
        if (mRecyclerView != null && mClickListener != null) {
            int position = mRecyclerView.getChildAdapterPosition(view);
            mClickListener.onChildClickListener(mRecyclerView, view, position);
        }
    }

    public interface onChildClickListener {
        void onChildClickListener(RecyclerView parent, View view, int position);
    }

    public void onChildClickListener(onChildClickListener listener) {
        mClickListener = listener;
    }

}
